import { defineStore } from 'pinia'
import { ref } from 'vue'
import * as workflowTemplateApi from '@/api/workflowTemplate'

export const useWorkflowTemplateStore = defineStore('workflowTemplate', () => {
  // 状态
  const templates = ref([])
  const currentTemplate = ref(null)
  const versionHistory = ref([])
  const loading = ref(false)
  const total = ref(0)

  // 获取模板列表
  const fetchTemplates = async (params) => {
    loading.value = true
    try {
      const data = await workflowTemplateApi.getWorkflowTemplateList(params)
      templates.value = data.items || []
      total.value = data.total || 0
      return data
    } finally {
      loading.value = false
    }
  }

  // 获取模板详情
  const fetchTemplateDetail = async (id) => {
    loading.value = true
    try {
      const data = await workflowTemplateApi.getWorkflowTemplateDetail(id)
      currentTemplate.value = data
      return data
    } finally {
      loading.value = false
    }
  }

  // 创建模板
  const createTemplate = async (data) => {
    return await workflowTemplateApi.createWorkflowTemplate(data)
  }

  // 更新模板（创建新版本）
  const updateTemplate = async (id, data) => {
    return await workflowTemplateApi.updateWorkflowTemplate(id, data)
  }

  // 删除模板
  const deleteTemplate = async (id) => {
    return await workflowTemplateApi.deleteWorkflowTemplate(id)
  }

  // 获取版本历史
  const fetchVersionHistory = async (templateId) => {
    loading.value = true
    try {
      const data = await workflowTemplateApi.getVersionHistory(templateId)
      versionHistory.value = data.versions || []
      return data
    } finally {
      loading.value = false
    }
  }

  // 获取指定版本详情
  const fetchVersionDetail = async (templateId, version) => {
    return await workflowTemplateApi.getVersionDetail(templateId, version)
  }

  // 版本对比
  const fetchVersionDiff = async (templateId, version1, version2) => {
    return await workflowTemplateApi.getVersionDiff(templateId, version1, version2)
  }

  // 回滚到指定版本
  const rollbackToVersion = async (templateId, targetVersion) => {
    return await workflowTemplateApi.rollbackToVersion(templateId, { target_version: targetVersion })
  }

  // 从模板创建实例
  const createInstanceFromTemplate = async (data) => {
    return await workflowTemplateApi.createInstanceFromTemplate(data)
  }

  // 复制实例
  const duplicateInstance = async (instanceId, data) => {
    return await workflowTemplateApi.duplicateInstance(instanceId, data)
  }

  // 实例另存为模板
  const saveInstanceAsTemplate = async (instanceId, data) => {
    return await workflowTemplateApi.saveInstanceAsTemplate(instanceId, data)
  }

  // 更新实例的定时调度
  const updateInstanceSchedule = async (instanceId, data) => {
    return await workflowTemplateApi.updateInstanceSchedule(instanceId, data)
  }

  return {
    // 状态
    templates,
    currentTemplate,
    versionHistory,
    loading,
    total,
    // 方法
    fetchTemplates,
    fetchTemplateDetail,
    createTemplate,
    updateTemplate,
    deleteTemplate,
    fetchVersionHistory,
    fetchVersionDetail,
    fetchVersionDiff,
    rollbackToVersion,
    createInstanceFromTemplate,
    duplicateInstance,
    saveInstanceAsTemplate,
    updateInstanceSchedule
  }
})
